//
//  HNSH_SearchFriendsNav.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/17.
//

#import "HNSH_SearchFriendsNav.h"

@interface HNSH_SearchFriendsNav () <UITextFieldDelegate>
/** 返回按钮 */
@property (nonatomic, strong)UIButton * backBtn;
/** 搜索框灰色背景 */
@property (nonatomic, strong)UIView * grayBgView;
/** 搜索图片 */
@property (nonatomic, strong)UIImageView * searchImgView;
/** 输入框 */
@property (nonatomic, strong)UITextField * textField;
/** 搜索按钮 */
@property (nonatomic, strong)UIButton * searchBtn;
@end

@implementation HNSH_SearchFriendsNav

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addAllViews];
        [self addAllLayout];
    }
    return self;
}

//MARK: Myself Methods
- (void)addAllViews{
    self.backgroundColor = [UIColor appWhiteLabelColor];
    [self addSubview:self.grayBgView];
    [self.grayBgView addSubview:self.searchImgView];
    [self.grayBgView addSubview:self.textField];
    [self addSubview:self.searchBtn];
}

- (void)addAllLayout{
    [self.grayBgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-60);
        make.height.mas_equalTo(34);
        make.top.mas_equalTo( (44 - 32) / 2);
    }];
    [self.searchImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.width.height.mas_equalTo(20);
        make.centerY.mas_equalTo(self.grayBgView);
    }];
    [self.textField mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.searchImgView.mas_right).offset(8);
        make.top.height.right.mas_equalTo(self.grayBgView);
    }];
    [self.searchBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(44);
        make.top.mas_equalTo(0);
    }];
}

//MARK: Myself Methods
- (void)searchBtnAction{
    if (self.searchBlock) {
        self.searchBlock(self.textField.text);
    }
}

//MARK: UITextFieldDelegate Methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (self.searchBlock) {
        self.searchBlock(textField.text);
    }
    return YES;
}

#pragma mark - Getter Methods
- (UIView *)grayBgView{
    if (!_grayBgView) {
        _grayBgView = [UIView new];
        _grayBgView.backgroundColor = [UIColor colorWithHexString:@"#F4F4F4"];
        _grayBgView.layer.cornerRadius = 16;
        _grayBgView.layer.masksToBounds = YES;
    }
    return _grayBgView;
}

- (UIImageView *)searchImgView{
    if (!_searchImgView) {
        _searchImgView = [UIImageView new];
        _searchImgView.image = [UIImage imageNamed:@"base_nav_search"];
    }
    return _searchImgView;
}

- (UITextField *)textField{
    if (!_textField) {
        _textField = [UITextField new];
        _textField.backgroundColor = [UIColor clearColor];
        _textField.textColor = [UIColor appBlackLabelColor];
        _textField.font = [UIFont systemFontOfSize:14];
        _textField.returnKeyType = UIReturnKeySearch;
        _textField.enablesReturnKeyAutomatically = YES;
        _textField.placeholder = @"请输入用户名/手机号";
        _textField.delegate = self;
    }
    return _textField;
}

- (UIButton *)searchBtn{
    if (!_searchBtn) {
        _searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
        [_searchBtn setTitleColor:[UIColor appBlack333LabelColor] forState:UIControlStateNormal];
        _searchBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_searchBtn addTarget:self action:@selector(searchBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBtn;
}

@end
